package com.ids.transfer.util;

public class SQLParseUtil {
	
	/**
	 * 解析返回需要的SQL语句
	 * 根据insert语句和主键(多个用","分开)
	 * 
	 * @param desSQL
	 *            insert SQL statement
	 * @param pk
	 *            primary key String
	 * @param type
	 *            SELECT OR UPDATE
	 * @return
	 */
	public static String parserSQL(String desSQL, String pk, String type) {
		//into 和一个空格insert into mytest3 name values(?)
		String tblName = desSQL.substring(desSQL.indexOf("into") + 5,
				desSQL.indexOf("(", desSQL.indexOf("into")));
		String fieldName = desSQL.substring(desSQL.indexOf("(") + 1,
				desSQL.indexOf(")"));

		//主键
		String[] cols = StrCharUtil.split(pk, ",");
		String col = "";
		String whereStr = "";
		//列名
		String[] fields = StrCharUtil.split(fieldName, ",");

		for (int i = 0; i < cols.length; i++)
			whereStr += (cols[i] + "=? and ");
		//这里去除主键为了进行更新
//		for (int i = cols.length; i < fields.length; i++)
//			col += fields[i] + "=?,";
		for (int i = 0; i < fields.length; i++)
			col += fields[i] + "=?,";
		col = col.substring(0, col.length() - 1);
		//减and+空格
		whereStr = whereStr.substring(0, whereStr.length() - 4);
		if (type.equalsIgnoreCase("update"))
			return "update " + tblName + " set " + col + " where " + whereStr;
		else if (type.equalsIgnoreCase("select"))
			return "select " + pk + " from " + tblName;
		else
			return "";
	}
	
	/**
	 * insert 和主键 组成select XX from table where XX=?
	 * @param desSQL
	 * @param pk
	 * @return
	 */
	public static String parserSQLAddWhereStatement(String desSQL, String pk) {

		String tblName = desSQL.substring(desSQL.indexOf("into") + 5,
				desSQL.indexOf("(", desSQL.indexOf("into")));
		String fieldName = desSQL.substring(desSQL.indexOf("(") + 1,
				desSQL.indexOf(")"));

		String[] cols = StrCharUtil.split(pk, ",");
		String col = "";
		String whereStr = "";

		String[] fields = StrCharUtil.split(fieldName, ",");

		for (int i = 0; i < cols.length; i++)
			whereStr += (cols[i] + "=? and ");
		for (int i = cols.length; i < fields.length; i++)
			col += fields[i] + "=?,";
		col = col.substring(0, col.length() - 1);
		whereStr = whereStr.substring(0, whereStr.length() - 4);

		return ("select " + fieldName + " from " + tblName + " where " + whereStr);
	}

	/**
	 * 根据insert和主键转为select语句
	 */
	public static String parserSQL(String desSQL, String pk) {
		//insert into table
		String tblName = desSQL.substring(desSQL.indexOf("into") + 5,
				desSQL.indexOf("(", desSQL.indexOf("into")));
		return "select " + pk + " from " + tblName;
	}

	public static String parserTableName(String desSQL) {
		//into 和一个空格insert into mytest3 name values(?)
		String tblName = desSQL.substring(desSQL.indexOf("into") + 5,
				desSQL.indexOf("(", desSQL.indexOf("into")));
		return tblName;
	}
	/**
	 * 记录在指定的关键字是否存在
	 * 
	 * @param srcRes
	 *            记录数组
	 * @param pkRes
	 *            关键字对应的数值
	 * @return 是/否
	 */
	public static boolean existsResultSet(String[] srcRes, String[] pkRes) {
		if (srcRes.length < pkRes.length)
			return false;
		for (int i = 0; i < pkRes.length; i++) {
			if (!srcRes[i].equals(pkRes[i]))
				return false;
		}
		return true;
	}

	public static void main(String[] args) {
		
		 System.out.println(parserSQL(
		 "insert into cust_info(id,name)values(?)"
		 ,"name","update")); 
//		 System.out.println(parserSQL(
//		 "insert into cust_info(compno,custid,name,address,eptype_id,"+
//		 "volt_id,trade_id,capacity,produce_mode)values(?,?,?,?,?,?,?,?,?)"
//		 ,"compno,custid","update")); Date dt=new Date();
//		 dt=DateTimeUtil.addDay(dt,-1); String
//		 t=DateTimeUtil.format(dt,"yyyy-MM-dd"); System.out.println(t);
		 
		String sql = parserSQLAddWhereStatement(
				"insert into temp_dw_f_custor_2(custor_id,dept4_id,aa,bb,cc)  values(?,?,?,?,?)",
				"custor_id");
		System.out.println(sql);

	}
}